//
//  BQMarketViewController.m
//  BeeQuick
//
//  Created by mac on 16/11/18.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQMarketViewController.h"
#import "BQYLLeftTableViewCell.h"
#import "BQYLRightTableViewCell.h"
#import "DSHTTPClient.h"
#import "BQYLDetailsModel.h"
#import <YYModel.h>


static NSString *leftCell = @"leftCell";
static NSString *rightCell = @"rightCell";

@interface BQMarketViewController () <UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak)UITableView *leftTableView;
@property(nonatomic,weak)UITableView *rigthTableView;

//左边的table
@property(nonatomic,strong)NSArray <NSString *> *nameArray;
//右边
@property(nonatomic,strong)NSArray <BQYLDetailsModel *> *modelList;

@end

@implementation BQMarketViewController

-(NSArray<NSString *> *)nameArray
{
    if (!_nameArray)
    {
        _nameArray = [NSArray array];
    }
    return _nameArray;
    
}

-(NSArray<BQYLDetailsModel *> *)modelList
{
    if (!_modelList)
    {
        _modelList = [NSArray array];
        
    }
    return _modelList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self setupUI];
}

#pragma mark - UI界面
- (void)setupUI
{
    //MARK:1. 左边table
    //左侧的tableView 不设置frame 约束中设置
    UITableView *leftTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    //添加到view
    [self.view addSubview:leftTableView];
    
    //MARK:2. 右边table
    UITableView *rightTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    //添加
    [self.view addSubview:rightTableView];
    
    //MARK:3. 数据源
    leftTableView.dataSource = self;
    rightTableView.dataSource = self;
    
    //MARK:4. 代理
    leftTableView.delegate = self;
    rightTableView.delegate = self;
    
    //MARK:5. 关联
    self.leftTableView = leftTableView;
    self.rigthTableView = rightTableView;
    
    //MARK:6. 注册
    [leftTableView registerClass:[BQYLLeftTableViewCell class] forCellReuseIdentifier:leftCell];
    [rightTableView registerNib:[UINib nibWithNibName:@"BQYLRightTableViewCell" bundle:nil]forCellReuseIdentifier:rightCell];
    
    //MARK:7. 约束
    [leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.view);
        make.width.mas_equalTo(86);
        make.bottom.equalTo(self.view);
    }];
    [rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.right.equalTo(self.view);
        make.left.equalTo(leftTableView.mas_right);
        make.bottom.equalTo(self.view);
    }];
    
    //MARK:8. 行高
    leftTableView.rowHeight = 45;
    rightTableView.estimatedRowHeight = 80;
    rightTableView.sectionHeaderHeight = 25;
    
    //MARK:9. 取消下划线
    leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

#pragma mark - 网络获取数据
-(void)getData
{
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    
    [dataDic setValue:@"5" forKey:@"call"];
    
    [DSHTTPClient postUrlString:@"supermarket.json.php" withParam:dataDic withSuccessBlock:^(id data) {
        //从DATA中获取左边名字
        NSArray *array = data[@"data"][@"categories"];
        
        
        
        NSMutableArray *nameArray = [NSMutableArray array];
        
        [array enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSString *name = obj[@"name"];
            
            [nameArray addObject:name];
            
        }];
        
        _nameArray = nameArray;
        
        //从data中获取右侧model数据
        NSArray *modelArray = data[@"data"][@"products"][@"a106"];
        
        
        
        NSMutableArray *mArray = [NSMutableArray array];
        [modelArray enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            
            
            BQYLDetailsModel *model = [BQYLDetailsModel yy_modelWithDictionary:obj];
            
            
            [mArray addObject:model];
            
        }];
        
        self.modelList = mArray.copy;
        
        
        
        
        //主队列刷UI
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.leftTableView reloadData];
            [self.rigthTableView reloadData];
            
        }];
        
    } withFailedBlock:^(NSError *error) {
        
    } withErrorBlock:^(NSString *message) {
        
    }];
    
    
    
}


#pragma mark - 数据源方法
//组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    if (tableView == _leftTableView)//左边table
    {
        return 1;
    }
    return _nameArray.count;//右侧
}

//行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _leftTableView)
    {
        return _nameArray.count;
    }
    
    
    return _modelList.count;
    
}

//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //定义CELL
    UITableViewCell *cell;
    if (tableView == _leftTableView)//左侧的table
    {
        cell = [tableView dequeueReusableCellWithIdentifier:leftCell forIndexPath:indexPath];
        cell.textLabel.text = _nameArray[indexPath.row];
        
    }
    else if(tableView == _rigthTableView)//右侧的table
    {
        
       cell = [tableView dequeueReusableCellWithIdentifier:rightCell forIndexPath:indexPath];
        
        BQYLDetailsModel *model = _modelList[indexPath.row];
        
        
        
         ((BQYLRightTableViewCell *)cell).model = model;
    }
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
